package cn.yemuysy.code.lingting.model;

import java.util.HashMap;
import java.util.Map;

/**
 * AC自动机Trie树节点
 * 支持字符和音节两种模式的统一节点结构
 */
public class TrieNode {
    
    /**
     * 节点值 - 支持Character或String类型
     */
    private Object nodeValue;
    
    /**
     * 子节点映射
     */
    private Map<Object, TrieNode> children;
    
    /**
     * 失效指针 - AC自动机核心
     */
    private TrieNode failPointer;
    
    /**
     * 是否为敏感词结尾
     */
    private boolean isEndOfWord;
    
    /**
     * 匹配的敏感词（如果是结尾节点）
     */
    private String matchedWord;
    
    /**
     * 构造函数
     */
    public TrieNode() {
        this.children = new HashMap<>();
        this.failPointer = null;
        this.isEndOfWord = false;
        this.matchedWord = null;
    }
    
    /**
     * 构造函数 - 带节点值
     */
    public TrieNode(Object nodeValue) {
        this();
        this.nodeValue = nodeValue;
    }
    
    /**
     * 添加子节点
     */
    public TrieNode addChild(Object key, TrieNode child) {
        children.put(key, child);
        return child;
    }
    
    /**
     * 获取子节点
     */
    public TrieNode getChild(Object key) {
        return children.get(key);
    }
    
    /**
     * 检查是否有指定子节点
     */
    public boolean hasChild(Object key) {
        return children.containsKey(key);
    }
    
    /**
     * 获取所有子节点的key
     */
    public Iterable<Object> getChildKeys() {
        return children.keySet();
    }
    
    /**
     * 设置失效指针
     */
    public void setFailPointer(TrieNode failPointer) {
        this.failPointer = failPointer;
    }
    
    /**
     * 获取失效指针
     */
    public TrieNode getFailPointer() {
        return failPointer;
    }
    
    /**
     * 设置为敏感词结尾
     */
    public void setEndOfWord(String word) {
        this.isEndOfWord = true;
        this.matchedWord = word;
    }
    
    /**
     * 检查是否为敏感词结尾
     */
    public boolean isEndOfWord() {
        return isEndOfWord;
    }
    
    /**
     * 获取匹配的敏感词
     */
    public String getMatchedWord() {
        return matchedWord;
    }
    
    /**
     * 获取节点值
     */
    public Object getNodeValue() {
        return nodeValue;
    }
    
    /**
     * 设置节点值
     */
    public void setNodeValue(Object nodeValue) {
        this.nodeValue = nodeValue;
    }
    
    /**
     * 获取子节点数量
     */
    public int getChildrenCount() {
        return children.size();
    }
    
    @Override
    public String toString() {
        return "TrieNode{" +
                "nodeValue=" + nodeValue +
                ", isEndOfWord=" + isEndOfWord +
                ", matchedWord='" + matchedWord + '\'' +
                ", childrenCount=" + children.size() +
                '}';
    }
} 